package com.wh.Ui;

import com.wh.Domian.User;
import com.wh.Util.CodeUtil;
import com.wh.Util.PopUpUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

//登录界面
public class LoginJframe extends JFrame implements MouseListener {
    //创建集合用于存储用户数据
    static ArrayList<User> allUsers = new ArrayList<>();

    {
        allUsers.add(new User( "133715", "wangheng6"));
    }

    //登录
    JButton logInJlabel = new JButton();
    //注册
    JButton registerJlabel = new JButton();

    //初始化文本输入框
    JTextField idText = new JTextField();
    JTextField passwordTest = new JPasswordField();
    JTextField codeText = new JTextField();

    JLabel showCode = new JLabel(CodeUtil.getCode());

    //验证码更新
    JLabel changeShowCode = new JLabel("换一张");

    public LoginJframe() {

        //初始化主界面
        initJFrame();

        //初始化界面内容
        initInterfaceContent();

        //显示页面
        this.setVisible(true);
    }

    //主界面
    public void initJFrame() {
        this.setSize(488, 430);
        this.setTitle("拼图 登录");
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    //界面内容
    public void initInterfaceContent() {


        //id
        JLabel idJlabel = new JLabel(new ImageIcon("image/login/用户名.png"));

        //加载id
        idJlabel.setBounds(116, 135, 51, 19);
        this.getContentPane().add(idJlabel);
        //加载id输入框
        idText.setBounds(195, 134, 200, 30);
        this.getContentPane().add(idText);

        //密码
        JLabel passwordJlabel = new JLabel(new ImageIcon("image/login/密码.png"));
        //加载密码
        passwordJlabel.setBounds(130, 195, 35, 18);
        this.getContentPane().add(passwordJlabel);
        //加载密码输入框
        passwordTest.setBounds(195, 194, 200, 30);
        this.getContentPane().add(passwordTest);

        //加载登录和注册
        logInJlabel.setBounds(110, 310, 128, 47);
        SwitchColors("login",true);
        logInJlabel.setBorderPainted(false);  //去除按钮边框
        logInJlabel.setContentAreaFilled(false);  //去除按钮背景
        logInJlabel.addMouseListener(this);  //给按钮添加鼠标绑定事件
        this.getContentPane().add(logInJlabel);
        registerJlabel.setBounds(270, 310, 128, 47);
        SwitchColors("register",true);
        registerJlabel.setBorderPainted(false);
        registerJlabel.setContentAreaFilled(false);
        registerJlabel.addMouseListener(this);
        this.getContentPane().add(registerJlabel);

        DisplayVerificationCode();

        //加载背景图片
        JLabel background = new JLabel(new ImageIcon("image/login/background.png"));
        //设置大小和位置
        background.setBounds(0, 0, 470, 390);
        //把背景图片添加到界面中
        this.getContentPane().add(background);
    }

    //验证码
    public void DisplayVerificationCode(){
        //加载验证码
        JLabel codeJlabel = new JLabel(new ImageIcon("image/login/验证码.png"));
        codeJlabel.setBounds(130, 250, 50, 30);
        this.getContentPane().add(codeJlabel);
        //加载验证码输入框
        codeText.setBounds(195,249,120,30);
        this.getContentPane().add(codeText);

        //加载显示验证码

        showCode.setBounds(320,249,120,30);
        this.getContentPane().add(showCode);

        //更换验证码
        changeShowCode.setBounds(370,249,120,30);
        changeShowCode.setForeground(new Color(0xA5E6E));
        changeShowCode.setFont(changeShowCode.getFont().deriveFont(16f));
        changeShowCode.addMouseListener(this);
        this.getContentPane().add(changeShowCode);
    }



    //登录和注册按钮变色
    public void SwitchColors(String str,boolean choose){

        switch (str){
            case "login" : {
                if (choose == true){
                    logInJlabel.setIcon(new ImageIcon("image/login/登录按钮.png"));
                }else {
                    logInJlabel.setIcon(new ImageIcon("image/login/登录按下.png"));
                }
                break;
            }
            case "register" : {
                if (choose == true){
                    registerJlabel.setIcon(new ImageIcon("image/login/注册按钮.png"));
                }else {
                    registerJlabel.setIcon(new ImageIcon("image/login/注册按下.png"));
                }
                break;
            }
        }

    }


    //鼠标事件的方法
    //单击
    @Override
    public void mouseClicked(MouseEvent e) {
        Object obj = e.getSource();

        //重新刷新验证码
        if (obj == changeShowCode){
            showCode.setText(CodeUtil.getCode());
        }

    }

    //按下不松
    @Override
    public void mousePressed(MouseEvent e){}

    //松开
    @Override
    public void mouseReleased(MouseEvent e) {
        Object obj = e.getSource();

        if (obj == logInJlabel) {
            //获取id和密码,验证码输入框中的文本
            String id = idText.getText();
            String pasword = passwordTest.getText();
            String code = codeText.getText();
            //对账号进行判断
            if(id.length() != 0 && pasword.length() != 0 ){
                if (idIndex(id) >= 0){
                    if (allUsers.get(idIndex(id)).getPassword().equals(pasword)){
                        if (showCode.getText().equalsIgnoreCase(code)){
                            this.setVisible(false);
                            new GemaJframe();
                        }else {
                            codeText.setText("");
                            showCode.setText(CodeUtil.getCode());
                            PopUpUtils.popWindows(PopUpUtils.ErrorTyoe.CATCHA_ERROR);
                        }
                    }else {
                        passwordTest.setText("");
                        codeText.setText("");
                        showCode.setText(CodeUtil.getCode());
                        PopUpUtils.popWindows(PopUpUtils.ErrorTyoe.AUTH_ERROR);
                    }
                }else {
                    idText.setText("");
                    passwordTest.setText("");
                    codeText.setText("");
                    showCode.setText(CodeUtil.getCode());
                    PopUpUtils.popWindows(PopUpUtils.ErrorTyoe.AUTH_ERROR);

                }
            }else {
                codeText.setText("");
                showCode.setText(CodeUtil.getCode());
                PopUpUtils.popWindows(PopUpUtils.ErrorTyoe.EMPTY_EEOR);
            }


            System.out.println("id：" + id);
            System.out.println("password:" + pasword);

        } else if (obj == registerJlabel) {
          //  SwitchColors("register",true);
            //关闭当前页面
            this.setVisible(false);
            //跳转进注册页面
            new RegisterJframe();
        }
    }

    //划入
    @Override
    public void mouseEntered(MouseEvent e) {
        Object obj = e.getSource();
        if (obj == logInJlabel) {
            SwitchColors("login",false);
        } else if (obj == registerJlabel) {
            SwitchColors("register",false);
        }

    }

    //划出
    @Override
    public void mouseExited(MouseEvent e) {
        Object obj = e.getSource();
        if (obj == logInJlabel) {
            SwitchColors("login",true);
        } else if (obj == registerJlabel) {
            SwitchColors("register",true);
        }
    }

    //通过id来找索引
    public static int idIndex(String id) {
        for (int i = 0; i < allUsers.size(); i++) {
            String index = allUsers.get(i).getId();
            if (index.equals(id)) {
                return i;  //存在返回索引
            }
        }
        return -1; //不存在返回-1
    }


}
